//
//  BanJiTableViewController.m
//  ZhiBoYuan
//
//  Created by coder_di on 2018/4/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "BanJiTableViewController.h"
#import "BanJiTableViewCell.h"
#import "BanJiDetailViewController.h"

@interface BanJiTableViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)NSMutableArray *dataArray;

@end

@implementation BanJiTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"我的班级";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self LoadData];
    
    [self addTableView];
    
    [self.tableView registerClass:[BanJiTableViewCell class] forCellReuseIdentifier:@"BanJiTableViewCell"];

}

-(void)LoadData{
    
    _dataArray = [NSMutableArray array];
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Class&a=myClass",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] intValue] == 200) {
            
            for (NSDictionary *dicc in responseObject[@"data"][@"class_list"]) {
                [_dataArray addObject:dicc];
            }
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showTextOnly:self.view message:responseObject[@"msg"] delay:2.0];
        }
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
}


-(void)addTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, Screenheight- NAVIGATION_BAR_HEIGHT ) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate        = self;
    _tableView.dataSource      = self;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    _tableView.estimatedRowHeight = 44.0f;//推测高度，必须有，可以随便写多少
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    
    [self.view addSubview:_tableView];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Navigation
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    
    return 0;
    
}

#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifity = @"BanJiTableViewCell";
    //    MyIndentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifity];
    BanJiTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[BanJiTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifity];
    }
    cell .accessoryType  = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    if(Teacher ){
        cell.nameLable.text = [_dataArray[indexPath.row]valueForKey:@"name"];
        cell.renshuLable.text = [NSString stringWithFormat:@"学生人数:%@人",[_dataArray[indexPath.row]valueForKey:@"student_num"]];
    }else{
        cell.nameLable.text = [_dataArray[indexPath.row]valueForKey:@"name"];
        cell.renshuLable.text = [NSString stringWithFormat:@"老师人数:%@人    学生人数:%@人",[_dataArray[indexPath.row]valueForKey:@"teacher_num"],[_dataArray[indexPath.row]valueForKey:@"student_num"]];
    
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80 * KscreenHeight;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BanJiDetailViewController *banjiDetailVC = [[BanJiDetailViewController alloc]init];
    banjiDetailVC.classID = [_dataArray[indexPath.row] valueForKey:@"id"];
    banjiDetailVC.titleName = [_dataArray[indexPath.row]valueForKey:@"name"];
    [self.navigationController pushViewController:banjiDetailVC animated:YES];
    
}


@end
